
*File to join game user data and PM2p5 data

***inputs:
* $Data/UserMonitorMatch`x'.dta
* $Data/GameUser_zd.dta
* $Data/aqs_sites_id.dta
* $Data/daily_PM2p5_1318.dta

***outputs:
* $Data/GameUser_pollution_zd.dta
* $Data/GameUserweighted_pollution_zd.dta




***1. GET MONITOR LOCATIONS FOR EACH DATE THAT WE NEED DATA FOR***

* tell us which monitors to match users to 
use $Data/UserMonitorMatch.dta, clear

	   
	* translate our monitor id to aq site info 
	merge m:1 id using $Data/aqs_sites_dailyid.dta
	drop if _merge ==2
	drop _merge
	   
	   
	* tell us which dates to get pollution data for 
	 joinby zip using $Data/GameUser_zd.dta, unmatched(master)
	drop _merge

	   
	***2. MERGE POLLUTION DATA FOR EACH DATE AND LOCATION AND MONITOR***

	merge m:1 statecode countycode sitenumber date using $Data/daily_PM2p5_1318.dta

	drop if _merge ==2 // 

	drop _merge


	***3. CALCULATE THE WEIGHTED AVERAGE OF PM2p5 BY ZIPCODE AND DATE***


	gen weight = 1/(aq_dist^2)


	gen distance_weight = mean_pollution*weight
	by zip date, sort: egen sumdistance_pm25 = total(distance_weight)
	by zip date, sort: egen sum_weight = total(weight)

	gen weighted_dailypm25 = sumdistance_pm25/sum_weight

	by zip date, sort: egen av_aq_distance = mean(aq_dist) if mean_pollution <. // keep average distance of matched monitor readings


save $Data/GameUser_pollution_zd.dta, replace





save $Data/GameUser_pollution_zd.dta, replace

drop if mean_pollution==.

keep zip date weighted_dailypm25  av_aq_distance
duplicates drop


save $Data/GameUserweighted_pollution_zd.dta, replace






